<?php
// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.view');

class rcbaseViewMotorTest extends JView
{
	function display($tpl = null)
	{
		$task_hdl = array(
			'addtest' => 'add_motor_test',
			'savetest' => 'save_motor_test',
			'testlist' => 'motor_test_list',
			'rateup'    => 'rate_up',
			'ratedown'  => 'rate_down',
		);
		$task = JRequest::getVar( 'task', 'motor_list' );
		$method = ($task_hdl[$task] ? $task_hdl[$task] : 'motor_test_list');
		$this->$method();
	}

	function motor_test_list($tpl = null){
		$model =& $this->getModel();
		$user =& JFactory::getUser();
		$data = array(
			'model' => $model,
			'user' => $user,
		);
		$this->assignRef( 'data',	$data );

		$doc =& JFactory::getDocument();
		$doc->setTitle( 'База данных по ВМГ::Тесты мотора' );
		$mainframe =& JFactory::getApplication();
		$pathway   =& $mainframe->getPathway();
		$pathway->addItem('Тесты мотора мотора', '');

		parent::setLayout('testlist');
		parent::display($tpl);
	}

	function add_motor_test($tpl = null){
		$model =& $this->getModel();
		$user =& JFactory::getUser();
		$motor_id = JRequest::getInt('id');
		$data = array(
			'model'    => $model,
			'user'     => $user,
			'motor_id' => $motor_id,
		);
		$this->assignRef( 'data',	$data );

		$doc =& JFactory::getDocument();
		$doc->setTitle( 'База данных по ВМГ::Тесты мотора::Новый тест' );
		$mainframe =& JFactory::getApplication();
		$pathway   =& $mainframe->getPathway();
		$pathway->addItem('МоторИнфо', '/?option=com_rcbase&view=motor&task=motor&id='.$data['motor_id']);
		$pathway->addItem('Новый тест мотора', '');

		parent::setLayout('addtest');
		parent::display($tpl);
	}

	function save_motor_test($tpl = null){
		$model =& $this->getModel();
		$model->saveTest();
		$user =& JFactory::getUser();
		$motor_id = JRequest::getInt('motor_id');
		$data = array(
			'model'    => $model,
			'user'     => $user,
			'motor_id' => $motor_id,
		);
		$this->assignRef( 'data',	$data );

		$doc =& JFactory::getDocument();
		$doc->setTitle( 'База данных по ВМГ::Тесты мотора::Новый тест' );
		$mainframe =& JFactory::getApplication();
		$pathway   =& $mainframe->getPathway();
		$pathway->addItem('МоторИнфо', '/?option=com_rcbase&view=motor&task=motor&id='.$data['motor_id']);
		$pathway->addItem('Новый тест мотора', '');

		parent::setLayout('savetest');
		parent::display($tpl);
	}

	function rate_up(){
		$this->rate_motor('up');
	}

	function rate_down(){
		$this->rate_motor('down');
	}

	function rate_motor($voit)
	{
		$model =& $this->getModel();
		$user =& JFactory::getUser();
		$motor_id = JRequest::getInt('mid');
		$test_id = JRequest::getInt('id');
		$voit_res = 'none';
		if ($user->guest){
			$voit_res = 'guest';
		} else {
			$voit_res = $model->setMotorTestRate($test_id,$voit);
		}
		$data = array(
			'user'     => $user,
			'motor_id' => $motor_id,
			'model'    => $model,
			'viot'     => $voit,
			'voit_res' => $voit_res,
		);
		$this->assignRef( 'data', $data );

		$doc =& JFactory::getDocument();
		$doc->setTitle( 'База данных по ВМГ::Тесты мотора::Голосование' );
		$mainframe =& JFactory::getApplication();
		$pathway   =& $mainframe->getPathway();
		$pathway->addItem('МоторИнфо', '/?option=com_rcbase&view=motor&task=motor&id='.$data['motor_id']);
		$pathway->addItem('Голосование', '');

		parent::setLayout('ratemotor');
		parent::display($tpl);
	}
}